from django.http import HttpResponse,JsonResponse
from django.shortcuts import render

# Create your views here.
from . import models


def admin(request):
    return render(request, "后端管理.html")

def view_score(request):
    student_scores = models.firstscore_table.objects.all()
    sum_english = 0
    sum_math = 0
    sum_major = 0
    sum_political = 0
    sum = 0
    for student_score in student_scores:
        sum += 1
        sum_english += student_score.englishscore
        sum_math += student_score.mathscore
        sum_major += student_score.majorscore
        sum_political += student_score.politicalscore
        
    score_info = {"ave_englishscore" : sum_english/sum,
                  "ave_mathscore" : sum_math/sum,
                  "ave_major" : sum_major/sum,
                  "ave_political" : sum_political/sum,
                  }
    return JsonResponse({'score_info': score_info})

# 将数据库中的数据获取到并且处理成图像，返回图像中的关键点给前端，使用前端进行渲染图像
def img_average_score(request):
    scores = models.firstscore_table.objects.all()
    math_score = []
    english_score = []
    major_score = []
    political_score = []
    for score in scores:
        math_score.append(score.mathscore)
        english_score.append(score.englishscore)
        major_score.append(score.majorscore)
        political_score.append(score.politicalscore)
    # 计算各个科目的平均分
    ave_mathscore = sum(math_score) / len(math_score)
    ave_englishscore = sum(english_score) / len(english_score)
    ave_major = sum(major_score) / len(major_score)
    ave_political = sum(political_score) / len(political_score)

    # 计算各个科目的分数段人数统计
    math_score_distribution = {
        "0-60": len([s for s in math_score if s < 60]),
        "60-70": len([s for s in math_score if 60 <= s < 70]),
        "70-80": len([s for s in math_score if 70 <= s < 80]),
        "80-90": len([s for s in math_score if 80 <= s < 90]),
        "90-100": len([s for s in math_score if 90 <= s < 100]),
        "100-110": len([s for s in math_score if 100 <= s < 110]),
        "110-120": len([s for s in math_score if 110 <= s < 120]),
        "120-130": len([s for s in math_score if 120 <= s < 130]),
        "130-140": len([s for s in math_score if 130 <= s < 140]),
        "140-150": len([s for s in math_score if 140 <= s <= 150]),
        
    }
    english_score_distribution = {
        "0-60": len([s for s in english_score if s < 60]),
        "60-70": len([s for s in english_score if 60 <= s < 70]),
        "70-80": len([s for s in english_score if 70 <= s < 80]),
        "80-90": len([s for s in english_score if 80 <= s < 90]),
        "90-100": len([s for s in english_score if 90 <= s <= 100])
    }
    major_score_distribution = {
        "0-60": len([s for s in major_score if s < 60]),
        "60-70": len([s for s in major_score if 60 <= s < 70]),
        "70-80": len([s for s in major_score if 70 <= s < 80]),
        "80-90": len([s for s in major_score if 80 <= s < 90]),
        "90-100": len([s for s in major_score if 90 <= s < 100]),
        "100-110": len([s for s in major_score if 100 <= s < 110]),
        "110-120": len([s for s in major_score if 110 <= s < 120]),
        "120-130": len([s for s in major_score if 120 <= s < 130]),
        "130-140": len([s for s in major_score if 130 <= s < 140]),
        "140-150": len([s for s in major_score if 140 <= s <= 150]),
    }
    political_score_distribution = {
        "0-60": len([s for s in political_score if s < 60]),
        "60-70": len([s for s in political_score if 60 <= s < 70]),
        "70-80": len([s for s in political_score if 70 <= s < 80]),
        "80-90": len([s for s in political_score if 80 <= s < 90]),
        "90-100": len([s for s in political_score if 90 <= s <= 100])
    }

    return JsonResponse({
        "score_info": {
            "ave_mathscore": ave_mathscore,
            "ave_englishscore": ave_englishscore,
            "ave_major": ave_major,
            "ave_political": ave_political,
            "math_score_distribution": math_score_distribution,
            "english_score_distribution": english_score_distribution,
            "major_score_distribution": major_score_distribution,
            "political_score_distribution": political_score_distribution
        }
    })


# 获取考生性别比例还有考生生源地的分布,并且将数据返回给前端渲染图表
from django.db.models import Count

def student_info(request):
    # 获取考生性别比例
    student_gender = list(models.registration_table.objects.values('gender').annotate(count=Count('gender')))
    
    # 获取考生生源地分布
    student_location = list(models.student_table.objects.values('origin').annotate(count=Count('origin')))
    
    # 准备数据格式,返回给前端渲染
    data = {
        'gender': student_gender,
        'location': student_location
    }
    
    return JsonResponse(data)